package net.lab1024.sa.admin.module.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import net.lab1024.sa.admin.module.flowable.domain.FlowableModel;
import net.lab1024.sa.admin.module.flowable.domain.bo.FlowableModelBo;
import net.lab1024.sa.admin.module.flowable.page.TableDataInfo;
import net.lab1024.sa.admin.module.flowable.service.IFlowableModelService;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.code.SystemErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 流程模型Controller
 *
 * @author ruoyi
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/flowable/model")
@Slf4j
public class FlowableModelController extends BaseController
{
    @Resource
    private IFlowableModelService flowableModelService;

    /**
     * 查询流程模型列表
     */
    @SaCheckPermission("flow:flow_model:list")
    @GetMapping("/list")
    public ResponseDTO<TableDataInfo> list(FlowableModelBo flowableModelBo)
    {
        startPage();
        List<FlowableModel> list = flowableModelService.selectFlowableModelList(flowableModelBo);
        return ResponseDTO.ok(getDataTable(list));
    }

    /**
     * 获取流程模型详细信息
     */
    @SaCheckPermission("flow:flow_model:query")
    @GetMapping(value = "/{modelId}")
    public ResponseDTO<FlowableModel> getInfo(@PathVariable("modelId") String modelId)
    {
        return ResponseDTO.ok(flowableModelService.selectFlowableModelByModelId(modelId));
    }

    /**
     * 新增流程模型
     */
    @SaCheckPermission("flow:flow_model:add")
    @PostMapping
    public ResponseDTO<Integer> add(@RequestBody FlowableModel flowableModel)
    {
        return ResponseDTO.ok(flowableModelService.insertFlowableModel(flowableModel));
    }

    /**
     * 修改流程模型
     */
    @SaCheckPermission("flow:flow_model:update")
    @PutMapping
    public ResponseDTO<Integer> edit(@RequestBody FlowableModel flowableModel)
    {
        return ResponseDTO.ok(flowableModelService.updateFlowableModel(flowableModel));
    }

    /**
     * 删除流程模型
     */
    @SaCheckPermission("flow:flow_model:delete")
	@DeleteMapping("/{modelIds}")
    public ResponseDTO<Integer> remove(@PathVariable String[] modelIds)
    {
        return ResponseDTO.ok(flowableModelService.deleteFlowableModelByModelIds(modelIds));
    }
    /**
     * 部署流程模型
     *
     * @param modelId 流程模型主键
     */
    @SaCheckPermission("flow:flow_model:deploy")
    @GetMapping("/deploy/{modelId}")
    public ResponseDTO<Object> deployModel(@PathVariable String modelId) {
        try {
            flowableModelService.deployModel(modelId);
        } catch (UnsupportedEncodingException e) {
            log.error("部署失败!",e);
        }
        return ResponseDTO.ok();
    }
    /**
     * 获取流程表单详细信息
     *
     * @param modelId 模型id
     */
    @SaCheckPermission("flow:flow_model:queryXml")
    @GetMapping(value = "/getBpmnXml/{modelId}")
    public ResponseDTO<Object> getBpmnXml(@NotNull(message = "主键不能为空") @PathVariable("modelId") String modelId) {
        try {
            return ResponseDTO.ok(flowableModelService.queryBpmnXmlById(modelId));
        } catch (UnsupportedEncodingException e) {
            log.error("获取模型xml失败!模型id:"+modelId,e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "获取模型xml失败!");
        }
    }
    /**
     * 查询流程模型版本历史列表
     *
     * @param modelBo 流程模型对象
     */
    @SaCheckPermission("flow:flow_model:historyList")
    @GetMapping("/historyList")
    public ResponseDTO<TableDataInfo> historyList(FlowableModelBo modelBo) {
        startPage();
        List<FlowableModel> list = flowableModelService.historyList(modelBo);
        TableDataInfo dataTable = getDataTable(list);
        Long count = flowableModelService.historyCount(modelBo);
        dataTable.setTotal(count);
        return ResponseDTO.ok(dataTable);
    }
    /**
     * 保存(更新或插入新版本)流程模型
     */
    @SaCheckPermission("flow:flow_model::save")
    @PostMapping("/save")
    public ResponseDTO<Object> save(@RequestBody FlowableModelBo modelBo) {
        flowableModelService.saveModel(modelBo);
        return ResponseDTO.ok();
    }
    /**
     * 设为最新流程模型
     *
     * @param modelId
     * @return
     */
    @SaCheckPermission("flow:flow_model:lastest")
    @PostMapping("/latest/")
    public ResponseDTO<Object> latest(@RequestParam String modelId) {
        try {
            flowableModelService.latestModel(modelId);
            return ResponseDTO.ok();
        } catch (UnsupportedEncodingException e) {
            log.error("设置最新版本失败!",e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR,"设置最新版本失败!");
        }

    }
}
